package com.qzsoft.system.common;

import org.apache.commons.lang3.StringUtils;

import java.util.Map;

public class CommonUtils {
    /**
     * 使用转义字符替换特殊字符
     * @param str
     * @return
     */
//    public static String replaceSqlLikeField(String str){
//        str = StringUtils.replace(str,"%","#%");
//        str = StringUtils.replace(str,"_","#_");
//        return str;
//    }

    public static String replaceSqlLikeField(String str) {
        String[] search = {"%", "_"};
        String[] replace = {"#%", "#_"};
        if (StringUtils.containsAny(str, '%', '_')) {
            return StringUtils.replaceEach(str, search, replace);
        }
        return str;
    }
    /**
     * 替换模糊搜索参数中的特殊字符
     *
     * @param params 封装模糊搜索参数的集合
     * @param args   获取集合参数值的key
     */
    public static void replaceSqlLikeField(Map<String, Object> params, String... args) {
        if (args != null && args.length > 0) {
            for (int i = 0; i < args.length; i++) {
                String key = args[i];
                if (params.get(key) != null && StringUtils.isNotBlank(params.get(key).toString())) {
                    params.put(key, replaceSqlLikeField(params.get(key).toString()));
                }
            }
        }
    }
    /**
     * 判断字符串是否包含特殊字符
     * @param str
     * @return
     */
    public static boolean check(String str){
        if (str.contains("%")){
            return true;
        }
        if (str.contains("_")){
            return true;
        }
        return false;
    }

    /**
     * 判断String 类型的查询参数是否存在
     * @param obj
     * @return
     */
    public static boolean checkStringObject(Object obj){
        boolean rs = false;
        if (obj != null && !("".equals((String)obj))){
            rs = true;
        }
        return rs;
    }
}
